package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.service.IGameAccountService;
import com.qianniu.napi.admin.service.IGameIdsService;
import com.qianniu.napi.admin.service.IGameLogService;
import com.qianniu.napi.admin.service.IGamesService;
import com.qianniu.napi.common.util.ObjectUtil;
import com.qianniu.napi.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *
 * </p>
 *
 * @author chenxuebin
 * @since 2022-02-17
 */
@Controller
public class GameIdsController {

    private static final Logger logger = LoggerFactory.getLogger(GameIdsController.class);

    @Autowired
    private IGameIdsService gameidsService;
    @Autowired
    private IGameAccountService gameAccountService;
    @Autowired
    private IGamesService gamesService;
    @Autowired
    private IGameLogService gamelogService;

    @RequestMapping("/api/gameids/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = gameidsService.selectPage(page, new EntityWrapper<GameIds>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/gameids/add")
    @ResponseBody
    public Jinfo gameidsAdd(GameIds gameids, HttpServletRequest req) {
        Jinfo info = new Jinfo();

        gameids.setCreatetime(new Date());
        gameids.insert();
        return info;
    }

    @RequestMapping("/api/gameids/modify")
    @ResponseBody
    public Jinfo gameidsUpdate(HttpServletRequest req, @RequestBody GameIds gameids) {
        Jinfo info = new Jinfo();
        logger.debug("--------gameidsUpdate=" + JSON.toJSONString(gameids));

        Games games = gamesService.selectOne(new EntityWrapper<Games>().eq("gametype", gameids.getGametype()));
        if (games == null) {
            info.setCode(4);
            return info;
        }
        Wrapper<GameIds> wrapper = new EntityWrapper<GameIds>()
                .eq("gametype", gameids.getGametype());
        if (!Strings.isNullOrEmpty(gameids.getUserid())) wrapper.eq("userid", gameids.getUserid());
        if (!Strings.isNullOrEmpty(gameids.getUsername())) wrapper.eq("username", gameids.getUsername());
        if (!Strings.isNullOrEmpty(gameids.getGametype2())) wrapper.eq("gametype2", gameids.getGametype2());

        GameIds game = gameidsService.selectOne(wrapper);

        if (game == null) {
            gameids.setCreatetime(new Date());
//            gameids.setCreator(Long.valueOf(gameids.getUserid()));
            gameids.setState(1);
            gameids.insert();
            game = gameids;
        } else {
            game.setMoney(gameids.getMoney());
            game.setModifytime(new Date());
//            game.setModifier(Long.valueOf(gameids.getUserid()));
            game.updateById();
        }
        info.setExtra(game.getState());
//        Wrapper<GameAccount> wrapper2 = new EntityWrapper<GameAccount>()
//                .eq("gametype", gameids.getGametype());
//        if (!Strings.isNullOrEmpty(gameids.getUserid())) wrapper2.eq("userid", gameids.getUserid());
//        if (!Strings.isNullOrEmpty(gameids.getUsername())) wrapper2.eq("username", gameids.getUsername());
//        if (!Strings.isNullOrEmpty(gameids.getGametype2())) wrapper.eq("gametype", gameids.getGametype2());
//        else wrapper.eq("gametype", gameids.getGametype());
//
//        logger.debug("--------gameidsUpdate gameAccount insert");
//        GameAccount gameAccount = gameAccountService.selectOne(wrapper2);
//        if (gameAccount == null) {
//            gameAccount = new GameAccount();
//            gameAccount.setGametype(!Strings.isNullOrEmpty(gameids.getGametype2()) ? gameids.getGametype2() : gameids.getGametype());
//            gameAccount.setUserid(gameids.getUserid());
//            gameAccount.setUsername(gameids.getUsername());
//            gameAccount.setPhone(gameids.getPhone());
//            gameAccount.setVersion(1l);
//            gameAccount.setMoney(gameids.getMoney());
//            gameAccount.setCreatetime(new Date());
//
//            gameAccount.insert();
//        }

        GameLog gamelog = new GameLog();
        gamelog.setGameid(game.getId());
        gamelog.setGametype(game.getGametype());
        gamelog.setGametype2(game.getGametype2());
        gamelog.setUserid(game.getUserid());
        gamelog.setUsername(game.getUsername());
        gamelog.setLogtype(0);
        gamelog.setYing(0);
        gamelog.setPing(0);
        gamelog.setShu(0);
        gamelog.setLiushui(0);
        gamelog.setRound(0);
        gamelog.setMoney(game.getMoney());
        gamelog.setZhuma(gameids.getZhuma());
        gamelog.setSn(StringUtil.getSnNo(""));
        gamelog.setCreatetime(new Date());
        gamelog.setVersion(games.getVersion());
        gamelog.setWebsite(gameids.getWebsite());
        gamelog.insert();
        logger.debug("--------gameidsUpdate gamelog insert");
        info.setData(gamelog.getSn());
        return info;
    }

    @RequestMapping("/api/gamelog/add")
    @ResponseBody
    public Jinfo gamelogAdd(HttpServletRequest req, @RequestBody GameLog gamelog) {
        Jinfo info = new Jinfo();
        logger.debug("--------gamelogAdd=" + JSON.toJSONString(gamelog));

        Wrapper<GameIds> wrapper = new EntityWrapper<GameIds>()
                .eq("gametype", gamelog.getGametype());

        if (!Strings.isNullOrEmpty(gamelog.getUserid())) wrapper.eq("userid", gamelog.getUserid());
        if (!Strings.isNullOrEmpty(gamelog.getUsername())) wrapper.eq("username", gamelog.getUsername());
        if (!Strings.isNullOrEmpty(gamelog.getGametype2())) wrapper.eq("gametype2", gamelog.getGametype2());
        GameIds game = gameidsService.selectOne(wrapper);

        gamelog.setGameid(game.getId());
        gamelog.setLogtype(1);
        gamelog.setCreatetime(new Date());
        gamelog.setWebsite(game.getWebsite());
        gamelog.insert();

        logger.debug("--------gamelogAdd gamelog insert");

        return info;
    }

    @RequestMapping("/api/gameids/detail")
    @ResponseBody
    public Jinfo getGameIdsDetailById(@RequestParam("token") String token,
                                      @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        GameIds gameids = gameidsService.selectById(id);
        info.setInfo(gameids);
        return info;
    }

}
